package com.liliya.servlet.admin;

import com.liliya.pojo.User;
import com.liliya.service.UserService;
import com.liliya.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/6 19:07
 */
@WebServlet(urlPatterns = "/admin/userdel")
public class UserDelServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        int step = 5;
        //一共有多少页
        Integer total = userService.findPage(step);

        int num = 1;
        String pageNum = req.getParameter("pageNum");
        if (pageNum != null) {
            num = Integer.parseInt(pageNum);
        }
        int start = (num - 1) * step + 1;
        int end = num * step;
        //上一页和下一页
        int pre = num - 1 < 1 ? 1 : num - 1;
        int next = num + 1 > total ? total : num + 1;
        System.out.println("total"+total);
        System.out.println("pre"+pre);
        System.out.println("next"+next);
        List<User> users = userService.findUsers(start, end);
        req.setAttribute("total",total);
        req.setAttribute("pre",pre);
        req.setAttribute("next",next);
        req.setAttribute("users",users);

        req.getRequestDispatcher("./userdel.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Set<String> set = map.keySet();
        //批量删除用户
        for (String s : set) {
            //然后这里就写删除的代码
            userService.deleteUser(s);
        }

        resp.sendRedirect(req.getContextPath()+"/success.jsp");
    }
}
